<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
import { index, shopLogin, index_data } from '@/api/shopsApi'
import FingerprintJS from '@fingerprintjs/fingerprintjs'
import crossClass from '@/utils/cross-domain.js'
export default {
  name: 'App',
  mounted() {
    this.$nextTick(() => {
      setTimeout(() => {
        this.chrackAddPlugin()
      }, 500)
    })
  },
  data() {
    return {
      addnew: true,
    }
  },
  methods: {
    chrackAddPlugin() {
      window.postMessage({ method: 'wt-wk--ckplg' })
      let addnew = true
      window.addEventListener('message', function (event) {
        if (event.data && event.data.method === 'wt-wk--ckplg-res') {
          console.log('noTer')
          addnew = false
        }
      })
      setTimeout(() => {
        // console.log('this.addnew', addnew)
        if (addnew) {
          this.$confirm('使用该功能需要安装最新版本插件', '提示', {
            confirmButtonText: '下载插件',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then((res) => {
              window.open('http://yywkhome.doulingyang.cn')
            })
            .catch((err) => {})
          return
        }
        this.initSet()
      }, 800)
    },
    async initSet() {
      //   const crossInstance = new crossClass()
      //   crossInstance.checkInstallPlugin()
      //   let crossInstanceState = await crossInstance.queryState()
      //   if (!crossInstanceState.CheckInstallPluginObj.isCheckInstallPlugin) {
      //     this.$confirm('使用该功能需要安装插件', '提示', {
      //       confirmButtonText: '下载插件',
      //       cancelButtonText: '取消',
      //       type: 'warning',
      //     })
      //       .then((res) => {
      //         window.open(
      //           'http://yywkhome.doulingyang.cn'
      //         )
      //       })
      //       .catch((err) => {})
      //     return
      //   }
      //   console.log(crossInstanceState.CheckInstallPluginObj, '11111')
      //   if (crossInstanceState.CheckInstallPluginObj.oldVersion) {
      //     this.$confirm('使用该功能需要安装最新版本插件', '提示', {
      //       confirmButtonText: '下载插件',
      //       cancelButtonText: '取消',
      //       type: 'warning',
      //     })
      //       .then((res) => {
      //         window.open(
      //           'http://yywkhome.doulingyang.cn'
      //         )
      //       })
      //       .catch((err) => {})
      //     return
      //   }
      const fpPromise = FingerprintJS.load()
      await fpPromise
        .then((fp) => fp.get())
        .then(async (result) => {
          const {
            languages,
            screenResolution,
            screenFrame,
            deviceMemory,
            ...components
          } = result.components
          const extendedComponents = {
            ...components,
            foo: { value: 'whatever' },
          }
          const fingerprintId = FingerprintJS.hashComponents(extendedComponents)
          localStorage.setItem('fingerprintId', fingerprintId)
        })
      index_data({})
        .then((res) => {})
        .catch((error) => {
          console.log(error)
        })
      console.log(1)
      //   await this.WT_requests()
      console.log(2)
      await this.index()
    },
    async WT_requests() {
      if (localStorage.getItem('Csrf') != null) {
        WT_request_wk({
          url: 'http://dlyt.doulingyang.cn/web/webpagehttp',
          method: 'post',
          data: JSON.stringify({
            url: 'https://compass.jinritemai.com/compass_api/config_center/shop/modify_index',
            method: 'post',
            query: {
              scene_identity: 'shop_traffic_analysis_channel_product',
              index_selected_list: [
                'product_show_ucnt',
                'product_click_ucnt',
                'product_show_cnt',
                'product_click_cnt',
                'product_click_cnt_per_user',
                'product_potential_cus_click_ucnt',
                'product_old_cus_click_ucnt',
                'product_potential_cus_click_ucnt_rate',
                'product_old_cus_click_ucnt_rate',
                'prod_wish_ucnt',
                'click_add_to_cart_ucnt',
                'boost_pv',
              ],
              _lid: '650472854445',
            },
          }),
          headers: {
            Accept: 'application/json, text/plain, */*',
            'Content-Type': 'application/json',
            'wt-X-Secsdk-Csrf-Token': localStorage.getItem('Csrf'),
            Authorization: `Bearer ${localStorage.getItem('Authorization')}`,
          },
        })
          .then((res) => {
            let { code, data, message } = JSON.parse(res.response)
            if (data.button == '前往订购' && data.code == 4390) {
              this.$confirm(data.msg, '提示', {
                confirmButtonText: '前往订购',
                cancelButtonText: '取消',
                type: 'warning',
              })
                .then((res) => {
                  window.open(data.url)
                })
                .catch((err) => {})
              localStorage.setItem('orderingAddress', data.url)
              localStorage.setItem('isPurchase', '0')
            } else if (
              code == 0 &&
              message == '请求成功' &&
              data.status == '403'
            ) {
              localStorage.setItem('isPurchase', '1')
            }
          })
          .catch((e) => {
            localStorage.setItem('isPurchase', '0')
          })
      }
      return true
    },
    async index() {
      const WT_requestcp = window.WT_request_wk
      if (WT_requestcp) {
        index()
          .then((res) => {
            localStorage.setItem('names', '')
            localStorage.setItem('shop_logo', '')
            localStorage.setItem('shop_id', '')
            localStorage.setItem('shop_phone', '')
            if (res.data.id) {
              localStorage.setItem('loginstatus', true)
              localStorage.setItem('names', res.data.shop_name)
              localStorage.setItem('shop_logo', res.data.shop_logo)
              localStorage.setItem('shop_id', res.data.id)
              localStorage.setItem('shop_phone', res.data.mobile_charge)
              this.shop(res.data.id)
            }
          })
          .catch((error) => {
            this.$message.error('使用该功能，需要打开并登录抖店。')
          })
      }
      return true
    },
    shop(id) {
      // shopLogin({
      //     shop_id: id
      // }).then((res) => {
      //     localStorage.setItem('Authorization', res.data.token)
      // })
    },
  },
}
</script>
